package com.tnsw.facebookautoliker;

import java.lang.reflect.Field;

import com.tnsw.coreutils.MyLog;
import com.tnsw.coreutils.database.RecordBase;
import com.tnsw.facebookautoliker.database.DBManager;
import com.tnsw.facebookautoliker.database.SelfInfo;
import com.tnsw.facebookautoliker.services.AutoLikerService;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.RelativeLayout;
import android.widget.Switch;
import android.widget.TextView;

public class MySwitcher implements OnCheckedChangeListener, OnClickListener {

	private String tag = "MySwitcher";
	private LayoutInflater inflater = null;
	private Context context;
	
	private String fieldName;
	private RecordBase record;
	private String header, detail;
	
	private DBManager dbm;
	
	private Switch swOption;
	private TextView tvItemHeader;
	private TextView tvItemDetail;
	
	public MySwitcher(Context context, String fieldName, RecordBase record, String header, String detail) {
		this.tag = tag + ":" + fieldName;
		this.context = context;
		this.fieldName = fieldName;
		this.record = record;
		this.header = header;
		this.detail = detail;
		
		if (context != null) {
			this.dbm = DBManager.getInstance(context);
			this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);	
		} 

	}
	
	private void setFieldValueString(RecordBase r, String fieldName, String fieldValue) {
		try {
			Class<? extends RecordBase> c = this.record.getClass();	
			Field f = c.getField(fieldName);
			f.set(r, fieldValue);
		} catch (Exception e) {
			MyLog.e(tag, "setFieldValueString(r=" + r.toString() + ", fieldName=" + fieldName + ", fieldValue=" + fieldValue + ") - " + e.toString(), e);
		}
	}
	
	private String getFieldValueString(RecordBase r, String fieldName) {
		String value = null;
		
		try {
			Class<? extends RecordBase> c = this.record.getClass();	
			Field f = c.getField(fieldName);
			value = (String) f.get(r);
		} catch (Exception e) {
			MyLog.e(tag, "getField(fieldName=" + fieldName + ", r=" + r.toString() + ") - " + e.toString(), e);
		}
		
		return(value);
	}
	
	public void updateSwitchStatus(RecordBase r) {
		this.record = r;
		//MyLog.d(tag, "updateSwitchStatus(r=" + r.toString() + ")");
		
		Class<? extends RecordBase> c = this.record.getClass();
		try {
			Field f = c.getField(this.fieldName);
			String value = (String) f.get(r);
			if (value.equals("Y")) {

				this.swOption.setChecked(true);	
				
			} else {
				this.swOption.setChecked(false);	

			}
		} catch (Exception e) {
			MyLog.e(tag, "updateSwitchStatus(" + r.toString() + ") - " + e.toString(), e);
		}
		
	}
	
	public RelativeLayout getView() {
		RelativeLayout l1 = (RelativeLayout) inflater.inflate(R.layout.myswitcher1, null);
		l1.setOnClickListener(this);
		
		this.swOption = (Switch) l1.findViewById(R.id.swOption);
		this.tvItemHeader = (TextView) l1.findViewById(R.id.tvItemHeader);
		this.tvItemDetail = (TextView) l1.findViewById(R.id.tvItemDetail);
		
		this.swOption.setOnCheckedChangeListener(this);
		this.swOption.setOnClickListener(this);
		
		this.tvItemHeader.setText(this.header);
		this.tvItemDetail.setText(this.detail);
		
		/*
		if (this.record != null) {
			String value = this.getFieldValueString(this.record, this.fieldName);
			MyLog.d(tag, "value on getView()=" + value);
			if (value != null) {
				if (value.equals("Y")) {
					this.swOption.setChecked(true);
					MyLog.d(tag, "Setting to true");
					
				} else {
					this.swOption.setChecked(false);
					MyLog.d(tag, "Setting to false");
				}
				
			} else {
				MyLog.e(tag, "Cannot find value from this field:" + this.fieldName);
			}
		}
		*/
		
		return(l1);
	}
	
	private void updateField(boolean b) {
		MyLog.d(tag, "updateField(b=" + b + ")");
		Class<? extends RecordBase> c = this.record.getClass();
		
		try {
			Field f = c.getField(this.fieldName);
			
			if (b == true) {
				f.set(this.record, "Y");
			} else {
				f.set(this.record, "N");	
			}
			
			this.dbm.updateRecordByPrimarykey("self_info", this.record, true);
			
			AutoLikerService aService = AutoLikerService.getInstance();
			if (aService != null) {
				aService.setSelfInfo((SelfInfo) this.record);
				MyLog.d(tag, "AutoLiker.getSelfInfo()=" + aService.getSelfInfo().toString());
			}
			
		} catch (Exception e) {
			MyLog.e(tag, "updateField - " + e.toString(), e);
		}

	}
	
	@Override
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		
		MyLog.d(tag, "onCheckChanged(isChecked=" + isChecked + ")");
		/*
		String value = this.getFieldValueString(this.record, this.fieldName);
		if (value.equals("Y")) {
			this.swOption.setChecked(true);
		} else {
			this.swOption.setChecked(false);
		}
		*/
		/*
		if (buttonView == this.swOption) {
			//this.updateField(isChecked);
			
			String value = "";
			if (this.swOption.isChecked() == true) {
				value = "Y";
				
			} else {
				value = "N";
			}
			MyLog.d(tag, "new value=" + value);
			
			this.setFieldValueString(this.record, this.fieldName, value);
			AutoLikerService aService = AutoLikerService.getInstance();
			if (aService != null) {
				aService.setSelfInfo((SelfInfo) this.record);
				MyLog.d(tag, this.record.toString());
			}
			
		}*/
	}

	@Override
	public void onClick(View v) {
		MyLog.d(tag, "onClick(v=" + v.toString() + ")");
		if (v == this.swOption) {
			String value = "";
			if (this.swOption.isChecked() == true) {
				value = "Y";
			} else {
				value = "N";
			}
			
			MyLog.d(tag, "valuevalue=" + value);
			
			this.setFieldValueString(this.record, this.fieldName, value);
			this.dbm.updateRecordByPrimarykey("self_info", this.record, true);
			
			AutoLikerService aService = AutoLikerService.getInstance();
			if (aService != null) {
				aService.setSelfInfo((SelfInfo) this.record);
				MyLog.d(tag, "XX->" + this.record.toString());
			}
		}
	}
	
	
}
